﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace Model
{
	public partial class DlgVerticalBridgeSlotDirection : System.Windows.Forms.Form
	{
		public int SelectionMethod { get; set; }

		public DlgVerticalBridgeSlotDirection()
		{
			this.InitializeComponent();
			this.xmlHelper.LoadXml(this.path, "Root");
			this.LoadDlgData();
		}

		private void DlgVerticalBridgeSlotDirection_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "VerticalBridgeSlotDirection"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "VerticalBridgeSlotDirection", "Params"));
			string value = "0";
			if (this.rdoBehind.Checked)
			{
				value = "1";
			}
			else if (this.rdoLeft.Checked)
			{
				value = "2";
			}
			else if (this.rdoFront.Checked)
			{
				value = "3";
			}
			else if (this.rdoAngle.Checked)
			{
				value = "4";
			}
			this.xmlHelper.SetAttribute("Angle", value);
			this.xmlHelper.SetAttribute("AngleValue", this.hytxtAngle.Text.Trim());
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "VerticalBridgeSlotDirection", "Params"));
			string attribute = this.xmlHelper.GetAttribute("Angle");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "0")
				{
					this.rdoRight.Checked = true;
				}
				else if (attribute == "1")
				{
					this.rdoBehind.Checked = true;
				}
				else if (attribute == "2")
				{
					this.rdoLeft.Checked = true;
				}
				else if (attribute == "3")
				{
					this.rdoFront.Checked = true;
				}
				else if (attribute == "4")
				{
					this.rdoAngle.Checked = true;
				}
			}
			string attribute2 = this.xmlHelper.GetAttribute("AngleValue");
			if (!string.IsNullOrEmpty(attribute2))
			{
				this.hytxtAngle.Text = attribute2;
			}
		}

		private void rdoAngle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoAngle.Checked)
			{
				this.hytxtAngle.Enabled = true;
				this.btnSel.Enabled = true;
				return;
			}
			this.hytxtAngle.Enabled = false;
			this.btnSel.Enabled = false;
		}

		private void hytxtAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && this.hytxtAngle.Text.Trim().IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private void btnSel_Click(object sender, EventArgs e)
		{
			this.SelectionMethod = 0;
			base.DialogResult = DialogResult.OK;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.SelectionMethod = 1;
		}

		private void DlgVerticalBridgeSlotDirection_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "PanelConnection.xml");

		private const string Root = "Root";

		private const string Node = "VerticalBridgeSlotDirection";

		private const string ChildNode = "Params";

		private const string Angle = "Angle";

		private const string AngleValue = "AngleValue";
	}
}
